package com.sbj.dto.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/22 11:50
 */
public class PackAgeMoney {
    /**
     *
     * @param remainRedNum 红包剩余数量
     * @param syMoney      剩余金额
     * @param minMoney     最小金额
     * @Author df
     * @Date 2020/9/19 15:45
     */
    public static BigDecimal getRedPackageMoney(int remainRedNum, BigDecimal syMoney, double minMoney) {
        Random random = new Random();
        BigDecimal bdRemainRedNum = BigDecimal.valueOf(remainRedNum);
        BigDecimal bdRemainRedNumTow = BigDecimal.valueOf(2);
        BigDecimal zero = BigDecimal.valueOf(0.0);
        BigDecimal max = syMoney.divide(bdRemainRedNum, 2, RoundingMode.FLOOR).multiply(bdRemainRedNumTow);
        // 随机获取金额
        Double money = random.nextDouble() * max.doubleValue();
        // 控制不能获取小于最小的金额
        money = money <= minMoney ? minMoney : money;
        BigDecimal bdMoney = BigDecimal.valueOf(money).setScale(2, RoundingMode.FLOOR);

        // 剩余金额小于等于0
        if (syMoney.compareTo(zero) < 1) {
            return zero;
        }
        // 剩余金额小于得到的随机金额
        if (syMoney.compareTo(bdMoney) == -1) {
            return syMoney;
        }
        return bdMoney;
    }

    public static void main(String[] args) {
        BigDecimal syMoney = BigDecimal.valueOf(80);
        BigDecimal total=BigDecimal.ZERO;
        for (int i = 100; i > 0; i--) {
            BigDecimal money = getRedPackageMoney(i, syMoney, 0.4);
            syMoney = syMoney.subtract(money).setScale(2);
            System.out.println("得到的红包金额：" + money + ",剩余的红包金额：" + syMoney);
            total=total.add(money);
        }

        System.out.println(total);
    }

}
